package com.wlc.springdemo.service.impl;

import com.wlc.springdemo.dao.CategoryMapper;
import com.wlc.springdemo.po.BookStore;
import com.wlc.springdemo.po.Category;
import com.wlc.springdemo.po.UserInfo;
import com.wlc.springdemo.service.BookStoreService;
import com.wlc.springdemo.service.UserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 王立朝
 * @date 2022/11/22
 * @description:
 */
@Service
@Slf4j
public class UserInfoServiceImpl implements UserInfoService {
    @Resource
    CategoryMapper categoryMapper;

    @Override
    public List<Category> findAll() {
        return categoryMapper.findAll();
    }

    @Resource
    private BookStoreService bookStoreService;

    /* Propagation.REQUIRED  支持当前事务，如果不存在，则创建新事务;  图书库存没有减少 */
    //@Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    @Override
    public void buyBook() {
        // 查询所有库存
        List<BookStore> bookStoreList = bookStoreService.getAllBookStore();
        log.info("bookStoreList =======> " + bookStoreList);
        // 所有的书籍，买一本
        // 账户信息
        UserInfo userInfo = new UserInfo();
        userInfo.setId(1);
        userInfo.setName("张三");
        userInfo.setAge(23);
        userInfo.setAccount(100);
        log.info("账户信息：=======》" + userInfo.toString());

        for (BookStore bookStore : bookStoreList) {
            int id = bookStore.getId();
            log.info("======》开始购买图书");
            // 减库存
            int result = bookStoreService.lessStock(bookStore, userInfo);

            log.info("书籍：" + id + " 减库存的结果为： " + result);
        }

    }
}
